/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.customer;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlInputText;
import mbean.AbstractMB;
import model.entity.Customer;
import model.entity.Role;
import model.entity.User;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.model.LazyDataModel;
import service.IUserService;
import service.InterfaceServiceLocal;
import utils.QueryStr;

/**
 *
 * @author dingnangen
 */
@Named(value = "customerCreateMB")
@SessionScoped
public class CustomerCreateMB extends AbstractMB implements Serializable {

    @EJB(beanName = "CustomerManager")
    private InterfaceServiceLocal<Customer> customerManager;
    @EJB(beanName = "RoleManager")
    private InterfaceServiceLocal<Role> roleManager;
    @EJB(beanName = "UserManager")
    private IUserService<User> userManager;
    private Customer currentCustomer;
    private User currentUser;

    /**
     * 初始化
     */
    public void init() {
        this.currentCustomer = new Customer();
        currentUser = new User();
        currentUser.setPassword("21218CCA77804D2BA1922C33E0151105");
        currentUser.setLastLogin(new Date());
        List<Role> roleList = new ArrayList();
        Role role = roleManager.find(1);
        System.out.println(role.getRoleName());
        roleList.add(role);
        currentUser.setRoleList(roleList);
        this.currentCustomer.setIduser(currentUser);
    }

    public void precreateCustomer() {
        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }
        init();
    }

    public void create() {
        if (!telvali()) {
            return;
        }
        userManager.create(currentUser);
        customerManager.create(currentCustomer);
        System.out.println("create finish");
        Messages.create(Faces.getResourceBundle("bundle").getString("saveSuccess")).add();
        init();
    }

    /**
     * Creates a new instance of CustomerCreateMB
     */
    public CustomerCreateMB() {
    }

    public Customer getCurrentCustomer() {
        return currentCustomer;
    }

    public void setCurrentCustomer(Customer currentCustomer) {
        this.currentCustomer = currentCustomer;
    }

    public void actiont() {

        System.out.println("address:" + currentCustomer.getAddress());
        // Faces.renderResponse();
    }

    public boolean telvali() {
        List params = new ArrayList();
        QueryStr queryStr = new QueryStr("User", 0);
        queryStr.addLikeCondition("where", "tel");
        params.add( currentUser.getTel().replaceAll("\\s*", "") );
        User findOneItem = userManager.findOneItem(queryStr.getQueryList(), params);
        if (findOneItem != null) {
            Messages.create(Faces.getResourceBundle("bundle").getString("unique")).add();
            return false;
        }
        return true;
    }

}
